home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Net / SmartIRC / messagehandler.php < prev   
PHP Script  |  2004-03-24  |  16KB  |  407 lines

  1. <?php
  2. /**
  3.  * $Id: messagehandler.php,v 1.25.2.2 2003/07/22 17:06:04 meebey Exp $
  4.  * $Revision: 1.25.2.2 $
  5.  * $Author: meebey $
  6.  * $Date: 2003/07/22 17:06:04 $
  7.  *
  8.  * Copyright (c) 2002-2003 Mirco "MEEBEY" Bauer <mail@meebey.net> <http://www.meebey.net>
  9.  * 
  10.  * Full LGPL License: <http://www.meebey.net/lgpl.txt>
  11.  * 
  12.  * This library is free software; you can redistribute it and/or
  13.  * modify it under the terms of the GNU Lesser General Public
  14.  * License as published by the Free Software Foundation; either
  15.  * version 2.1 of the License, or (at your option) any later version.
  16.  *
  17.  * This library is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20.  * Lesser General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU Lesser General Public
  23.  * License along with this library; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  25.  */
  26.  
  27. class Net_SmartIRC_messagehandler extends Net_SmartIRC_irccommands
  28. {
  29.     /* misc */
  30.     function _event_ping(&$ircdata)
  31.     {
  32.         $this->_pong(substr($ircdata->rawmessage, 5));
  33.     }
  34.     
  35.     function _event_error(&$ircdata)
  36.     {
  37.         if ($this->_autoretry == true) {
  38.             $this->reconnect();
  39.         } else {
  40.             $this->disconnect(true);
  41.         }
  42.     }
  43.     
  44.     function _event_join(&$ircdata)
  45.     {
  46.         if ($this->_channelsyncing == true) {
  47.             if ($this->_nick == $ircdata->nick) {
  48.                 $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: joining channel: '.$ircdata->channel, __FILE__, __LINE__);
  49.                 $channel = &new Net_SmartIRC_channel();
  50.                 $channel->name = $ircdata->channel;
  51.                 $this->_channels[strtolower($channel->name)] = &$channel;
  52.                 
  53.                 $this->who($channel->name);
  54.                 $this->mode($channel->name);
  55.                 $this->ban($channel->name);
  56.             }
  57.             
  58.             $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: '.$ircdata->nick.' joins channel: '.$ircdata->channel, __FILE__, __LINE__);
  59.             $channel = &$this->_channels[strtolower($ircdata->channel)];
  60.             $user = &new Net_SmartIRC_channeluser();
  61.             $user->nick = $ircdata->nick;
  62.             $user->ident = $ircdata->ident;
  63.             $user->host = $ircdata->host;
  64.             
  65.             $this->_adduser($channel, $user);
  66.             $this->who($user->nick);
  67.         }
  68.     }
  69.     
  70.     function _event_part(&$ircdata)
  71.     {
  72.         if ($this->_channelsyncing == true) {
  73.             $this->_removeuser($ircdata);
  74.         }
  75.     }
  76.     
  77.     function _event_kick(&$ircdata)
  78.     {
  79.         if ($this->_channelsyncing == true) {
  80.             $this->_removeuser($ircdata);
  81.         }
  82.     }
  83.     
  84.     function _event_quit(&$ircdata)
  85.     {
  86.         if ($this->_channelsyncing == true) {
  87.             $this->_removeuser($ircdata);
  88.         }
  89.     }
  90.     
  91.     function _event_nick(&$ircdata)
  92.     {
  93.         if ($this->_channelsyncing == true) {
  94.             $newnick = substr($ircdata->rawmessageex[2], 1);
  95.             $lowerednewnick = strtolower($newnick);
  96.             $lowerednick = strtolower($ircdata->nick);
  97.             
  98.             foreach ($this->_channels as $channelkey => $channelvalue) {
  99.                 // loop through all channels
  100.                 $channel = &$this->_channels[$channelkey];
  101.                 foreach ($channel->users as $userkey => $uservalue) {
  102.                     // loop through all user in this channel
  103.                     
  104.                     if ($ircdata->nick == $uservalue->nick) {
  105.                         // found him
  106.                         // time for updating the object and his nickname
  107.                         $channel->users[$lowerednewnick] = $channel->users[$lowerednick];
  108.                         $channel->users[$lowerednewnick]->nick = $newnick;
  109.                         
  110.                         if ($lowerednewnick != $lowerednick) {
  111.                             unset($channel->users[$lowerednick]);
  112.                         }
  113.                         
  114.                         // he was maybe op or voice, update comming
  115.                         if (isset($channel->ops[$ircdata->nick])) {
  116.                             $channel->ops[$newnick] = $channel->ops[$ircdata->nick];
  117.                             unset($channel->ops[$ircdata->nick]);
  118.                         }
  119.                         if (isset($channel->voices[$ircdata->nick])) {
  120.                             $channel->voices[$newnick] = $channel->voices[$ircdata->nick];
  121.                             unset($channel->voices[$ircdata->nick]);
  122.                         }
  123.                         
  124.                         break;
  125.                     }
  126.                 }
  127.             }
  128.         }
  129.     }
  130.     
  131.     function _event_mode(&$ircdata)
  132.     {
  133.         // check if its own usermode
  134.         if ($ircdata->rawmessageex[2] == $this->_nick) {
  135.             $this->_usermode = substr($ircdata->rawmessageex[3], 1);
  136.         } else if ($this->_channelsyncing == true) {
  137.             // it's not, and we do channel syching
  138.             $channel = &$this->_channels[strtolower($ircdata->channel)];
  139.             $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: updating channel mode for: '.$channel->name, __FILE__, __LINE__);
  140.             $mode = $ircdata->rawmessageex[3];
  141.             $parameters = array_slice($ircdata->rawmessageex, 4);
  142.             
  143.             $add = false;
  144.             $remove = false;
  145.             $channelmode = '';
  146.             $modelength = strlen($mode);
  147.             for ($i = 0; $i < $modelength; $i++) {
  148.                 switch($mode[$i]) {
  149.                     case '-':
  150.                         $remove = true;
  151.                         $add = false;
  152.                     break;
  153.                     case '+':
  154.                         $add = true;
  155.                         $remove = false;
  156.                     break;
  157.                     // user modes
  158.                     case 'o':
  159.                         $nick = array_shift($parameters);
  160.                         $lowerednick = strtolower($nick);
  161.                         if ($add) {
  162.                             $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: adding op: '.$nick.' to channel: '.$channel->name, __FILE__, __LINE__);
  163.                             $channel->ops[$nick] = true;
  164.                             $channel->users[$lowerednick]->op = true;
  165.                         }
  166.                         if ($remove) {
  167.                             $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: removing op: '.$nick.' to channel: '.$channel->name, __FILE__, __LINE__);
  168.                             unset($channel->ops[$nick]);
  169.                             $channel->users[$lowerednick]->op = false;
  170.                         }
  171.                     break;
  172.                     case 'v':
  173.                         $nick = array_shift($parameters);
  174.                         $lowerednick = strtolower($nick);
  175.                         if ($add) {
  176.                             $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: adding voice: '.$nick.' to channel: '.$channel->name, __FILE__, __LINE__);
  177.                             $channel->voices[$nick] = true;
  178.                             $channel->users[$lowerednick]->voice = true;
  179.                         }
  180.                         if ($remove) {
  181.                             $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: removing voice: '.$nick.' to channel: '.$channel->name, __FILE__, __LINE__);
  182.                             unset($channel->voices[$nick]);
  183.                             $channel->users[$lowerednick]->voice = false;
  184.                         }
  185.                     break;
  186.                     case 'k':
  187.                         $key = array_shift($parameters);
  188.                         if ($add) {
  189.                             $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: stored channel key for: '.$channel->name, __FILE__, __LINE__);
  190.                             $channel->key = $key;
  191.                         }
  192.                         if ($remove) {
  193.                             $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: removed channel key for: '.$channel->name, __FILE__, __LINE__);
  194.                             $channel->key = '';
  195.                         }
  196.                     break;
  197.                     default:
  198.                         // channel modes
  199.                         if ($mode[$i] == 'b') {
  200.                             $hostmask = array_shift($parameters);
  201.                             if ($add) {
  202.                                 $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: adding ban: '.$hostmask.' for: '.$channel->name, __FILE__, __LINE__);
  203.                                 $channel->bans[$hostmask] = true;
  204.                             }
  205.                             if ($remove) {
  206.                                 $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: removing ban: '.$hostmask.' for: '.$channel->name, __FILE__, __LINE__);
  207.                                 unset($channel->bans[$hostmask]);
  208.                             }
  209.                         } else {
  210.                             $this->log(SMARTIRC_DEBUG_CHANNELSYNCING, 'DEBUG_CHANNELSYNCING: storing unknown channelmode ('.$mode.') in channel->mode for: '.$channel->name, __FILE__, __LINE__);
  211.                             if ($add) {
  212.                                 $channel->mode .= $mode[$i];
  213.                             }
  214.                             if ($remove) {
  215.                                 $channel->mode = str_replace($mode[$i], '', $channel->mode);
  216.                             }
  217.                         }
  218.                 }
  219.             }
  220.         }
  221.     }
  222.     
  223.     function _event_topic(&$ircdata)
  224.     {
  225.         if ($this->_channelsyncing == true) {
  226.             $channel = &$this->_channels[strtolower($ircdata->rawmessageex[2])];
  227.             $channel->topic = $ircdata->message;
  228.         }
  229.     }
  230.     
  231.     function _event_privmsg(&$ircdata)
  232.     {
  233.         if ($ircdata->type == SMARTIRC_TYPE_CTCP) {
  234.             // substr must be 1,4 because of \001 in CTCP messages
  235.             if (substr($ircdata->message, 1, 4) == 'PING') {
  236.                 $this->message(SMARTIRC_TYPE_CTCP, $ircdata->nick, 'PING '.substr($ircdata->message, 5, -1));
  237.             } elseif (substr($ircdata->message, 1, 7) == 'VERSION') {
  238.                 if (!empty($this->_ctcpversion)) {
  239.                     $versionstring = $this->_ctcpversion.' | using '.SMARTIRC_VERSIONSTRING;
  240.                 } else {
  241.                     $versionstring = SMARTIRC_VERSIONSTRING;
  242.                 }
  243.                 
  244.                 $this->message(SMARTIRC_TYPE_CTCP, $ircdata->nick, 'VERSION '.$versionstring);
  245.             }
  246.         }
  247.     }
  248.     
  249.     /* rpl_ */
  250.     function _event_rpl_welcome(&$ircdata)
  251.     {
  252.         $this->_loggedin = true;
  253.         $this->log(SMARTIRC_DEBUG_CONNECTION, 'DEBUG_CONNECTION: logged in', __FILE__, __LINE__);
  254.         
  255.         // updating our nickname, that we got (maybe cutted...)
  256.         $this->_nick = $ircdata->rawmessageex[2];
  257.     }
  258.     
  259.     function _event_rpl_motdstart(&$ircdata)
  260.     {
  261.         $this->_motd[] = $ircdata->message;
  262.     }
  263.     
  264.     function _event_rpl_motd(&$ircdata)
  265.     {
  266.         $this->_motd[] = $ircdata->message;
  267.     }
  268.     
  269.     function _event_rpl_endofmotd(&$ircdata)
  270.     {
  271.         $this->_motd[] = $ircdata->message;
  272.     }
  273.     
  274.     function _event_rpl_umodeis(&$ircdata)
  275.     {
  276.         $this->_usermode = $ircdata->message;
  277.     }
  278.     
  279.     function _event_rpl_channelmodeis(&$ircdata) {
  280.         if ($this->_channelsyncing == true && $this->isJoined($ircdata->channel)) {
  281.             $mode = $ircdata->rawmessageex[4];
  282.             $parameters = array_slice($ircdata->rawmessageex, 5);
  283.             
  284.             $ircdata->rawmessageex = array( 0 => '',
  285.                                             1 => '',
  286.                                             2 => '',
  287.                                             3 => $mode);
  288.             
  289.             foreach ($parameters as $value) {
  290.                 $ircdata->rawmessageex[] = $value;
  291.             }
  292.             
  293.             // let _mode() handle the received mode
  294.             $this->_event_mode($ircdata);
  295.         }
  296.     }
  297.     
  298.     function _event_rpl_whoreply(&$ircdata)
  299.     {
  300.         if ($this->_channelsyncing == true) {
  301.             if ($ircdata->channel == '*') {
  302.                 $nick = $ircdata->rawmessageex[7];
  303.                 // we got who info without channel info, so we need to search the user
  304.                 // on all channels and update him
  305.                 foreach ($this->_channels as $channel) {
  306.                     if ($this->isJoined($channel->name, $nick)) {
  307.                         $ircdata->channel = $channel->name;
  308.                         $this->_event_rpl_whoreply($ircdata);
  309.                     }
  310.                 }
  311.             } else {
  312.                 $channel = &$this->_channels[strtolower($ircdata->channel)];
  313.                 
  314.                 $user = &new Net_SmartIRC_channeluser();
  315.                 $user->ident = $ircdata->rawmessageex[4];
  316.                 $user->host = $ircdata->rawmessageex[5];
  317.                 $user->server = $ircdata->rawmessageex[6];
  318.                 $user->nick = $ircdata->rawmessageex[7];
  319.                 
  320.                 $user->op = false;
  321.                 $user->voice = false;
  322.                 $user->ircop = false;
  323.                 
  324.                 $usermode = $ircdata->rawmessageex[8];
  325.                 $usermodelength = strlen($usermode);
  326.                 for ($i = 0; $i < $usermodelength; $i++) {
  327.                     switch ($usermode[$i]) {
  328.                         case 'H':
  329.                             $user->away = false;
  330.                         break;
  331.                         case 'G':
  332.                             $user->away = true;
  333.                         break;
  334.                         case '@':
  335.                             $user->op = true;
  336.                         break;
  337.                         case '+':
  338.                             $user->voice = true;
  339.                         break;
  340.                         case '*':
  341.                             $user->ircop = true;
  342.                         break;
  343.                     }
  344.                 }
  345.                  
  346.                 $user->hopcount = substr($ircdata->rawmessageex[9], 1);
  347.                 $user->realname = implode(array_slice($ircdata->rawmessageex, 10), ' ');
  348.                 
  349.                 $this->_adduser($channel, $user);
  350.             }
  351.         }
  352.     }
  353.     
  354.     function _event_rpl_namreply(&$ircdata)
  355.     {
  356.         if ($this->_channelsyncing == true) {
  357.             $channel = &$this->_channels[strtolower($ircdata->channel)];
  358.             
  359.             $userarray = explode(' ', substr($ircdata->message, 0, -1));
  360.             $userarraycount = count($userarray);
  361.             for ($i = 0; $i < $userarraycount; $i++) {
  362.                 $user = &new Net_SmartIRC_channeluser();
  363.                 
  364.                 $usermode = substr($userarray[$i], 0, 1);
  365.                 switch ($usermode) {
  366.                     case '@':
  367.                         $user->op = true;
  368.                         $user->nick = substr($userarray[$i], 1);
  369.                     break;
  370.                     case '+':
  371.                         $user->voice = true;
  372.                         $user->nick = substr($userarray[$i], 1);
  373.                     break;
  374.                     default:
  375.                         $user->nick = $userarray[$i];
  376.                 }
  377.                 
  378.                 $this->_adduser($channel, $user);
  379.             }
  380.         }
  381.     }
  382.     
  383.     function _event_rpl_banlist(&$ircdata)
  384.     {
  385.         if ($this->_channelsyncing == true && $this->isJoined($ircdata->channel)) {
  386.             $channel = &$this->_channels[strtolower($ircdata->channel)];
  387.             $hostmask = $ircdata->rawmessageex[4];
  388.             $channel->bans[$hostmask] = true;
  389.         }
  390.     }
  391.     
  392.     function _event_rpl_topic(&$ircdata)
  393.     {
  394.         if ($this->_channelsyncing == true) {
  395.             $channel = &$this->_channels[strtolower($ircdata->channel)];
  396.             $topic = substr(implode(array_slice($ircdata->rawmessageex, 4), ' '), 1);
  397.             $channel->topic = $topic;
  398.         }
  399.     }
  400.     
  401.     /* err_ */
  402.     function _event_err_nicknameinuse(&$ircdata)
  403.     {
  404.         $this->_nicknameinuse();
  405.     }
  406. }
  407. ?>